package gameUi
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import gameUi.Event.ChipChooseEvent;

	public class ChipBox
	{
		private var chipArray:Array;//筹码数组
		private var leftIndex:int;//记录最左边筹码的数组索引
		private var chooseIndex:int;//记录当前选择筹码的数组索引
		private var _chooseTypeArray:Array;//记录当前选择筹码的索引数组
		private var _chooseChipNum:int;//当前选择筹码面值
		
		/**
		 * 筹码动作管理类
		 **/
		public function ChipBox()
		{
			chipBoxInit();
			addListener();
		}
		
		/**
		 * 当前选择筹码面值
		 **/
		public function get chooseChipNum():int
		{
			return _chooseChipNum;
		}

		/**
		 * 记录当前选择筹码的帧数组
		 **/
		public function get chooseTypeArray():Array
		{
			return _chooseTypeArray;
		}

		private function chipBoxInit():void{
			chipArray = new Array();
			
			creatChip(3,8,1);
			creatChip(4,11,5);
			creatChip(5,12,10);
			creatChip(6,14,25);
			creatChip(7,16,100);
			creatChip(8,19,500);
			creatChip(9,20,1000);
			
			for(var i:int = 0;i < 6;i++){
				chipArray[i].x = BlackJack._gameUi.chipBox["p"+(i+1).toString()].x;
				chipArray[i].y = BlackJack._gameUi.chipBox["p"+(i+1).toString()].y;
			}
			chipArray[6].x = BlackJack._gameUi.chipBox["p0"].x;
			chipArray[6].y = BlackJack._gameUi.chipBox["p0"].y;
			chipArray[6].visible = false;
			chipArray[5].visible = false;
			//默认选择第一个筹码
			chooseIndex = 0;
			_chooseChipNum = 1;
			_chooseTypeArray = [3,8];
			((chipArray[0] as MovieClip).getChildAt(0) as Chip).chooseThis();
		}
		
		private function addListener():void{
			BlackJack._gameUi.chipBtn.rightBtn.addEventListener(MouseEvent.CLICK,clickRightBtn);
			BlackJack._gameUi.chipBtn.leftBtn.addEventListener(MouseEvent.CLICK,clickLeftBtn);
			BlackJack.eventTower.addEventListener(ChipChooseEvent.CHOOSE,chooseOneChip);
		}
		
		private function removeListener():void{
			BlackJack._gameUi.chipBtn.rightBtn.removeEventListener(MouseEvent.CLICK,clickRightBtn);
			BlackJack._gameUi.chipBtn.leftBtn.removeEventListener(MouseEvent.CLICK,clickLeftBtn);
		}
		
		//实例化筹码
		private function creatChip(colorType:int,numType:int,chipNum:int):void{
			var chipMC:MovieClip = new MovieClip();
			var chip:Chip = new Chip(colorType,numType,chipArray.length,chipNum);
			chipMC.addChild(chip);
			BlackJack._gameUi.chipBox.addChild(chipMC);
			chipArray.push(chipMC);
		}
		
		//选择一个筹码
		private function chooseOneChip(e:ChipChooseEvent):void{
			((chipArray[chooseIndex] as MovieClip).getChildAt(0) as Chip).chooseOver();
			chooseIndex = e.index;
			_chooseChipNum = e.chipNum;
			_chooseTypeArray = e.chipTypeArr;
		}
		
		//点击右移按钮
		private function clickRightBtn(e:MouseEvent):void{
			removeListener();
			var i:int;
			var n:int = 0;
			for(i = leftIndex;i < 7;i++){
				TweenLite.to(chipArray[i],0.5,{x:BlackJack._gameUi.chipBox["p"+n].x , y:BlackJack._gameUi.chipBox["p"+n].y});
				n++
			}
			for(i = 0;i < leftIndex - 1;i++){
				TweenLite.to(chipArray[i],0.5,{x:BlackJack._gameUi.chipBox["p"+n].x , y:BlackJack._gameUi.chipBox["p"+n].y});
				n++
			}
			var tempNum:int = leftIndex-1 == -1 ? 6 : leftIndex-1;
			chipArray[tempNum].x =  BlackJack._gameUi.chipBox["p6"].x;
			chipArray[tempNum].y =  BlackJack._gameUi.chipBox["p6"].y;
			tempNum = leftIndex-2 < 0 ? leftIndex+5 : leftIndex-2;
			chipArray[tempNum].visible = true;
			chipArray[leftIndex].visible = false;
			setTimeout(addListener,500);
			leftIndex = leftIndex == 6? 0:leftIndex+1;
		}
		
		//点击左移按钮
		private function clickLeftBtn(e:MouseEvent):void{
			removeListener();
			var i:int;
			var n:int = 2;
			var tempNum:int;
			for(i = leftIndex;i < leftIndex+5;i++){
				tempNum = i>6?i-7:i
				TweenLite.to(chipArray[tempNum],0.5,{x:BlackJack._gameUi.chipBox["p"+n].x , y:BlackJack._gameUi.chipBox["p"+n].y});
				n++
			}
			var tempNum0:int = leftIndex+5>6?leftIndex-2:leftIndex+5;
			for(i = 0;i < 2;i++){
				if(n > 6) n -= 7;
				tempNum = tempNum0>6?tempNum0-7:tempNum0;
				TweenLite.to(chipArray[tempNum],0.5,{x:BlackJack._gameUi.chipBox["p"+n].x , y:BlackJack._gameUi.chipBox["p"+n].y});
				n++;
				tempNum0++;
			}
			tempNum = leftIndex+5 > 6 ? leftIndex - 2 : leftIndex + 5;
			chipArray[tempNum].x =  BlackJack._gameUi.chipBox["p0"].x;
			chipArray[tempNum].y =  BlackJack._gameUi.chipBox["p0"].y;
			tempNum = leftIndex-1 < 0 ? leftIndex+6 : leftIndex-1;
			chipArray[tempNum].visible = true;
			tempNum = leftIndex+4 >6 ? leftIndex - 3 : leftIndex + 4;
			chipArray[tempNum].visible = false;
			setTimeout(addListener,500);
			leftIndex = leftIndex == 0? 6:leftIndex-1;
		}
		
	}
}